<?php

class CpstructureController extends Core_Controller_Action 
{
	private $_categries;
	
	public function init() {
	
		$this->_categries = new Categries();
	}
	
	public function indexAction() {
		$this->_forward( 'list', 'cpstructure' );
	}
	
	public function itemAction() {
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		$type = intval( $this->_request->getParam( 'type', 0 ) );

		$this->view->list = $this->_categries->getList( $type, 'AND c.parent_id = 0 AND c.c_id != ' . $id )->toArray();
		
		if( $this->_request->isPost() ) {
		
			$post = $this->_request->getPost();

			if( empty( $post['name_pl'] ) ) {
				$this->view->err_name_pl = 'Podaj nazwe';
			} else {

				if( $id ) {
					$item = $this->_categries->getItem( $id )->getFirst();
					
					foreach( $post as $key => $value ) {
	
						if( strlen( $value ) > 0 ) {
							$item->{$key} = $value;
						}
					}
					$item->save();

					$this->_redirect( 'cpstructure/list/type/' . $type . '/oper/2' );
				} else {
					foreach( $post as $key => $value ) {
	
						if( strlen( $value ) > 0 ) {
							$this->_categries->{$key} = $value;
						}
					}
					$this->_categries->save();
					
					$this->_redirect( 'cpstructure/list/type/' . $type . '/oper/1' );
				}
			}

		} else {
			if( $id ) {
				$item = $this->_categries->getItem( $id )->toArray();

				foreach( $item[0] as $key => $value ) {
	
					$this->view->{$key} = $value;
				}
			}
		
		}
		
		$this->assignType( $type );
	}
	
	public function listAction() {
		if( $this->_request->isPost() ) {
			$this->_helper->layout->disableLayout();
			$this->_helper->viewRenderer->setNoRender();
			
			$post = $this->_request->getPost();
			
			$i = 1;
			foreach( $post['data'] as $value ) {
				$Categries = new Categries();
				$item = $Categries->getItem( $value['name'] )->getFirst();
				$item->order_item = $i;
				$item->save();
				
				$i++;
			}

			echo Zend_Json::encode( array( 'status' => 1 ) );
			
		} else {
		
			$type = intval( $this->_request->getParam( 'type', 0 ) );
			$page = intval( $this->_request->getParam( 'page', 1 ) );
			
			$this->view->categories = $this->_categries;
			
			$pagerLayout = $this->_categries->getListWithPager( $type, $page, $this->view->baseUrl() );
			
			$list = $pagerLayout->execute();
			
			$temp = array();
			foreach( $list->toArray() as $value ) {
	
				if( $value['parent_id'] < 1 ) {
					$temp[$value['c_id']]['elem'] = $value;
				} else {
					$temp[$value['parent_id']]['items'][] = $value;
				}
			
			}
			$this->view->list = $temp;
			
			$this->view->pager = $pagerLayout;
			
			
			$this->assignType( $type );
			
		}
	}
	
	public function deleteAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$type = intval( $this->_request->getParam( 'type', 0 ) );
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		if( $id > 0 ) {
			$this->_categries->deleteItem( $id );
			
			$this->_redirect( 'cpstructure/list/type/' . $type . '/oper/4' );
		} else {
			$this->_redirect( 'cpstructure/list/type/' . $type . '/oper/3' );
		}
	}
	
	public function assignType( $type ) {
		
		$this->view->type = $type;
		
		switch( $type ) {
			case 0 :
				$this->view->yih2 = 'Kategorie sklepu';
				break;
			case 1 :
				$this->view->yih2 = 'Kategorie specjalne';
				break;
			case 2 :
				$this->view->yih2 = 'Kategorie aktualności';
				break;
			case 3 :
				$this->view->yih2 = 'Kategorie autodetailing';
				break;
			case 5 :
				$this->view->yih2 = 'Kategorie współpraca';
				break;
			case 6 :
				$this->view->yih2 = 'Kategorie usługi';
				break;
		}
	}
}